/*
 * LandmarkBoxSelector.cpp
 *
 *  Created on: Oct 14, 2013
 *      Author: filipe
 */

#include "LandmarkBoxSelector.h"


void
LandmarkBoxSelector::_DefaultConstructor()
{
	_disparity = 0.0;
}


void
LandmarkBoxSelector::_GetMeanDisparityOfTheBox()
{
	int row, col, num_valid_disparities;
	double disparity, mean_disparity;

	mean_disparity = 0.0;
	num_valid_disparities = 0;

	for (int i = 0; i < _box.height; i++)
	{
		for (int j = 0; j < _box.width; j++)
		{
			row = _box.y + i;
			col = _box.x + j;

			disparity = (double) _disparity_image.at<unsigned char>(row, col);

			if (disparity > 0.01)
			{
				mean_disparity += disparity;
				num_valid_disparities++;
			}
		}
	}

	_disparity = mean_disparity / ((double) num_valid_disparities);
}


LandmarkBoxSelector::LandmarkBoxSelector()
{
	_DefaultConstructor();
}


LandmarkBoxSelector::LandmarkBoxSelector(cv::Mat &reference, cv::Mat &disparity) : BoxSelector(reference)
{
	_DefaultConstructor();
	_disparity_image = cv::Mat(disparity);
}


LandmarkBoxSelector::LandmarkBoxSelector(std::string &reference_filename, std::string &disparity_filename) : BoxSelector(reference_filename)
{
	_DefaultConstructor();
	_disparity_image = cv::imread(disparity_filename, cv::IMREAD_GRAYSCALE);
}


LandmarkBoxSelector::~LandmarkBoxSelector()
{
}


void
LandmarkBoxSelector::Loop(void)
{
	BoxSelector::Loop();
	_GetMeanDisparityOfTheBox();
}


double
LandmarkBoxSelector::GetDisparity(void) const
{
	return _disparity;
}


